import { login, logout, getInfo, getcomponent } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";

const user = {
  state: {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
    role: "",
    realname: "",
    permissions: [],
    userId: "",
    user: {},
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_ROLE: (state, role) => {
      state.role = role;
    },
    SET_REALNAME: (state, realname) => {
      state.realname = realname;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
    },
    SET_USER: (state, name) => {
      state.userId = name;
    },
    SET_USER_INFO: (state, user) => {
      state.user = user;
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      console.log(userInfo, "userInfo");
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const scope = "";
      const grant_type = "password";
      const publicKeyId = userInfo.publicKeyId;
      const validateCode = userInfo.validCode;
      const deviceId = userInfo.deviceId;
      return new Promise((resolve, reject) => {
        login(
          username,
          password,
          grant_type,
          scope,
          publicKeyId,
          validateCode,
          deviceId
        )
          .then((res) => {
            setToken(res.obj.accessToken);
            commit("SET_TOKEN", res.obj.accessToken);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token)
          .then((res) => {
            const user = res.obj;
            // const avatar =
            //   user.userPhoto == "" || user.userPhoto == null
            //     ? require("@/assets/images/profile.jpg")
            //     : process.env.VUE_APP_BASE_API + user.userPhoto;
            const avatar = user.userHeader == "" || user.userHeader == null || user.userHeader ==0
            ? require("@/assets/portal/ico-default.png")
              : user.userHeader==1?require('@/assets/portal/ico-bigHead.png')
                :user.userHeader==2?require('@/assets/portal/ico-female.png'):require("@/assets/portal/ico-default.png")
            if (user.sysRoleList && user.sysRoleList.length > 0) {
              // 验证返回的roles是否是一个非空数组
              commit("SET_ROLES", user.sysRoleList);
              // commit('SET_PERMISSIONS', res.permissions)
            } else {
              commit("SET_ROLES", ["ROLE_DEFAULT"]);
            }
            commit("SET_NAME", user.username);
            commit("SET_USER", user.baseId);
            commit("SET_ROLE", user.role);
            commit("SET_REALNAME", user.realname);
            commit("SET_USER_INFO", user);
            commit("SET_AVATAR", avatar);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    //获取按钮
    getComponent({ commit }, path) {
      return new Promise((resolve, reject) => {
        getcomponent(path)
          .then((res) => {
            if (res.code == 0) {
              commit("SET_PERMISSIONS", res.obj);
              resolve();
            } else {
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            debugger;
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_PERMISSIONS", []);
            commit("SET_REALNAME", '');
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },
  },
};

export default user;
